#define JMBC_INSIDE
#include "mlLoop.h"
#include "Core/id.h"

// Context

#define STORE ID_LARGE_STORE

static MLLoop *store[STORE];

void
mlLoopCreateContext(void)
{
	idClearStore(MLLoop, STORE, store);
}

// Generation / Deletion

static
void
Init(MLLoop *group)
{
}

static
void
Term(MLLoop *group)
{
}

void
mlGenLoops(unsigned int n, unsigned int *loops)
{
	idGenObjects(MLLoop, STORE, store, Init, n, loops);
}

void
mlDeleteLoops(unsigned int n, unsigned int *loops)
{
	idDeleteObjects(store, Term, n, loops);
}

// Query

float
mlLoopValue(unsigned int id)
{
	MLLoop *loop;

	loop = store[id];

	return loop->value;
}

// Operation

void
mlLoopLoad(unsigned int id, float start, float end, float inc, float init)
{
	MLLoop *loop;

	loop = store[id];

	loop->start = start;
	loop->end = end;
	loop->inc = inc;
	loop->value = init;
}

void
mlLoopSet(unsigned int id, float value)
{
	MLLoop *loop;

	loop = store[id];

	loop->value = value;
}

void
mlLoopInc(unsigned int id)
{
	MLLoop *loop;

	loop = store[id];

	loop->value += loop->inc;

	if(loop->value >= loop->end)
		loop->value = loop->start;
}

void
mlLoopDec(unsigned int id)
{
	MLLoop *loop;

	loop = store[id];

	loop->value -= loop->inc;

	if(loop->value <= loop->start)
		loop->value = loop->end;
}
